{
 "cells": [
  {
   "cell_type": "raw",
   "id": "b3c0251e",
   "metadata": {},
   "source": [
    "### 购物车程序项目：\n",
    " 要求：1、运行程序后，让用户输入支付宝余额，然后打印我们的商品列表给用户。\n",
    "     2、让用户输入商品编号进行商品的购买。\n",
    "     3/用户选择商品后，检查用户的余额是否够，若够则直接扣款，不够则提醒用户。"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "a1b1c437",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "请输入你的支付宝余额:271831\n",
      "----goods list-----\n",
      "商品编号：0,商品名称:phone13,商品价格:5600\n",
      "商品编号：1,商品名称:macbook,商品价格:13000\n",
      "商品编号：2,商品名称:ipad pro,商品价格:9800\n",
      "请输入商品的编号0\n",
      "【phone13】已经加入您的购物车，您的余额还有266231元\n",
      "----goods list-----\n",
      "商品编号：0,商品名称:phone13,商品价格:5600\n",
      "商品编号：1,商品名称:macbook,商品价格:13000\n",
      "商品编号：2,商品名称:ipad pro,商品价格:9800\n"
     ]
    }
   ],
   "source": [
    "# list练习 购物车练习\n",
    "goods_list = [[\"phone13\",5600],[\"macbook\",13000],[\"ipad pro\",9800]] #每一个商品的内容是一个多个值的值，可选用list\n",
    "user_shopping_list = []       #存放用户购买的商品\n",
    "alipay = int(input(\"请输入你的支付宝余额:\"))\n",
    "# info = '''----goods list-----\n",
    "# 商品编号：%s,商品价格:%s,商品价格:%s\n",
    "# 商品编号：%s,商品价格:%s,商品价格:%s\n",
    "# 商品编号：%s,商品价格:%s,商品价格:%s\n",
    "# 商品编号：%s,商品价格:%s,商品价格:%s\n",
    "# '''%(0,goods_list[0][0],goods_list[0][1],\n",
    "# 1,goods_list[1][0],goods_list[1][1],\n",
    "# 2,goods_list[2][0],goods_list[2][1],\n",
    "#\n",
    "# )\n",
    "#print(info)\n",
    "# list的长度 len（）\n",
    "\n",
    "#执行循环 用户没退出时应该继续循环\n",
    "while True:   # 将实现 过程的循环\n",
    "    print('----goods list-----')\n",
    "    for i in range(len(goods_list)):        # 打印商品信息，打印商品的次数是由商品列表的长度决定的（len（goods_list））\n",
    "        #样式（商品编号：xx,商品名称:xx,商品价格:xx）\n",
    "        print(\"商品编号：%s,商品名称:%s,商品价格:%s\"%(i,goods_list[i][0],goods_list[i][1]))  # 占位符：%s\n",
    "\n",
    "    #用户输入商品编号，实现商品的查询\n",
    "    choice = input(\"请输入商品的编号\")  #input输入的内容默认是字符串str 例如输入2 显示”2“\n",
    "    if choice.isdigit():    #isdigit判断字符串是否是数值\n",
    "        choice = int(choice) # 如果是数值，把它变成真正的int\n",
    "        #判断编号是否存在\n",
    "        if choice >= len(goods_list):\n",
    "            print(\"【您输入的商品不存在，请检查后再次输入商品编号】\")\n",
    "            #检查用户余额>商品价格\n",
    "        else: #对比商品编号，找到对应商品 商品价格：goods_list[choice][1],\n",
    "            if alipay > goods_list[choice][1]: #买得起 扣钱并将商品加入到用户购物车里\n",
    "                alipay = alipay - goods_list[choice][1]  #余额发生变化，要扣除\n",
    "                user_shopping_list.append(goods_list[choice]) #把商品加入用户的购物列表里用append增加\n",
    "                print(\"【%s】已经加入您的购物车，您的余额还有%s元\"%(goods_list[choice][0],alipay))\n",
    "            else:\n",
    "                print(\"您的余额不足，请更换支付方式~\")\n",
    "    elif choice == \"q\":\n",
    "        #如果用户输入q，则用户退出购买，将打印用户已买商品和用户余额\n",
    "            print(\"您的购物车：\",user_shopping_list,'/n',\"您的账户余额：%s\"%(alipay))\n",
    "            break\n",
    "    else:\n",
    "        print(\"请输入正确的商品编号,输入的商品不存在\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e472e9bc",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "bd0b393c",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
